package cn.zucc.edu.handyzucc.controller;

import cn.zucc.edu.handyzucc.entity.Shop;
import cn.zucc.edu.handyzucc.service.ShopService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

@RestController
@RequestMapping("/shop")
public class ShopController {
    @Autowired
    private ShopService shopService;

    @RequestMapping(value = "getShopsByType", method = RequestMethod.POST)
    private Map<String, Object> getShopsByType(String shop_type) {
        Map<String, Object> modelMap = new HashMap<>();
        List<Shop> shops = shopService.getShopsByType(shop_type);
        modelMap.put("code", "0");
        modelMap.put("info", "查询成功");
        modelMap.put("data", shops);
        return modelMap;
    }

    @RequestMapping(value = "getShopDetail", method = RequestMethod.POST)
    private Map<String, Object> getShopDetail(Integer shop_id) {
        Map<String, Object> modelMap = new HashMap<>();
        Shop shop = shopService.getShopDetail(shop_id);
        modelMap.put("code", "0");
        modelMap.put("info", "查询成功");
        modelMap.put("data", shop);
        return modelMap;
    }

    @RequestMapping(value = "getAllShop", method = RequestMethod.GET)
    private Map<String, Object> getAllShop() {
        Map<String, Object> modelMap = new HashMap<>();
        List<Shop> shops = shopService.getAllShop();
        modelMap.put("code", "0");
        modelMap.put("data", shops);
        return modelMap;
    }

    @RequestMapping(value = "deleteShop", method = RequestMethod.POST)
    private Map<String, Object> deleteShop(@RequestBody Map<String, Object> map) {
        Map<String, Object> modelMap = new HashMap<>();
        if (shopService.deleteShop(Integer.parseInt(map.get("shop_id").toString())))
            modelMap.put("code", "0");
        else modelMap.put("code", "-1");
        return modelMap;
    }


    @RequestMapping(value = "addShop", method = RequestMethod.POST)
    private Map<String, Object> addShop(@RequestBody Map<String, Object> map) {
        Map<String, Object> modelMap = new HashMap<>();
        Shop shop = new Shop(map.get("name").toString(), map.get("location").toString(), map.get("phone").toString(),
                map.get("business").toString(), map.get("type").toString(), Double.parseDouble(map.get("latitude").toString()),
                Double.parseDouble(map.get("longitude").toString()));
        int shopId = shopService.addShop(shop);
        modelMap.put("code", "0");
        modelMap.put("shopId", shopId);
        return modelMap;
    }

    @RequestMapping(value = "updateShop", method = RequestMethod.POST)
    private Map<String, Object> updateShop(@RequestBody Map<String, Object> map) {
        Map<String, Object> modelMap = new HashMap<>();
        Shop shop = new Shop(Integer.parseInt(map.get("id").toString()), map.get("name").toString(), map.get("location").toString(), map.get("phone").toString(),
                map.get("business").toString(), map.get("type").toString(), Double.parseDouble(map.get("latitude").toString()),
                Double.parseDouble(map.get("longitude").toString()));
        if (shopService.updateShop(shop))
            modelMap.put("code","0");
        else map.put("code","-1");
        return modelMap;
    }

}
